package com.console.operations.planmgr;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanReleasePage;

	/**
		@author 王嘉犇
		@name 理财计划发布
		@type 动作类
	 */

public class FinancePlanRelease extends OperationBase {

	public FinancePlanRelease(UITestBase tb){
		super(tb);
	}
	
	public boolean isinserted = false;
	
	public void release(String name, String amount, String rate, String period, String repaytype, String securitylevel, String activitytype, String matchtype, String summary) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(8999);
        x = x+1000;
        String tempstr = datestr + String.valueOf(x);
        if(DBUtil.isQueryData("select * from fiz_plan where vc_name = ?", new Object[]{name})){
        	DBUtil.update("update fiz_plan set vc_name = ? where vc_name = ?", new Object[]{tempstr, name});
        }
		switchToDefaultContent();
		FinancePlanReleasePage fprp = new FinancePlanReleasePage(tb);
		//页面加载，直到名称输入框可视，可以进行下一步操作
		waitForElementDisplayed(fprp.nameInput);
		//名称输入框点击并输入名称
		fprp.nameInput.click();
		fprp.nameInput.type(name);
		pause(800);
		//限额输入框输入限额
		fprp.amountInput.type(amount);
		pause(800);
		//清空年化收益率输入框
		fprp.rateInput.clear();
		pause(100);
		//年化收益率输入框输入年化收益率
		fprp.rateInput.type(rate);
		pause(800);
		//清空理财周期输入框
		fprp.periodInput.clear();
		pause(100);
		//理财周期输入框输入周期
		fprp.periodInput.type(period);
		pause(800);
		//让还款类型选择框处于可视
		//还款类型选择框选择还款类型
		fprp.paytypestagedselect.selectOptionByText(repaytype);
		pause(800);
		//让安全等级选择框处于可视
		//安全等级选择框选择安全等级
		fprp.securityLevelselect.selectOptionByText(securitylevel);
		pause(800);
		//让活动类型选择框处于可视
		//活动类型选择框选择活动类型
		fprp.activityTypeSelect.selectOptionByText(activitytype);
		pause(800);
		//让定向匹配选择框处于可视
		//定向匹配选择框选择定向匹配
		fprp.matchTypeSelect.selectOptionByText(matchtype);
		pause(800);
		//让概述输入框处于可视
		//概述输入框输入概述
		fprp.summaryInput.type(summary);
		pause(800);
		selectFrame("ueditor_0");
		pause(800);
		switchToDefaultContent();
		//点击发布理财计划按钮
		fprp.releaseFinancePlanButton.click();
		waitForPageContainsStr("成功");
		//判断理财计划是否成功插入plan表
		isinserted = DBUtil.isQueryData("select *\n"
									  + "from fiz_plan\n"
									  + "where vc_name = ?\n"
									  + "  and dc_status = ?", new Object[]{name, "00"});
		if(!isinserted){
			log.error("data not insert into the database");
			return;
		}
	}
}
